#include "Socket.hpp"
#include <iostream>
#include <memory>
#include <sys/wait.h>
#include <functional>

using namespace SocketModule;
using namespace LogModule;

using ioservice_t = std::function<void(std::shared_ptr<Socket>& sock, InetAddr& client)>;

// 主要解决：连接的问题，IO通信的问题
// 细节: TcpServer,不需要关心自己未来传递的信息是什么
// 网络版本的计算器，长服务

class TcpServer
{
public:

    TcpServer(uint16_t port)
        :  _port(port),
           _listensockptr(std::make_unique<TcpSocket>()),
           _isrunning(false)
    {
        _listensockptr->BuildTcpSocketMethod(_port);
    }

    void Start(ioservice_t callback)
    {
        _isrunning = true;
        while(_isrunning)
        {
            InetAddr client;
            auto sock = _listensockptr->Accept(&client);
            if(sock == nullptr)
            {
                continue;
            }
            LOG(LogLevel::DEBUG) << "accept success..." << client.StringAddr();

            // //sock && client
            pid_t id = fork();
            if(id < 0)
            {   
                LOG(LogLevel::FATAL) << "fork error...";
                exit(FORK_ERR);
            }
            else if(id == 0)
            {
                //子进程 -> listensock
                _listensockptr->Close();
                if(fork() > 0)
                    exit(OK);
                //孙子进程
                //在执行任务，已经是孤儿了
                callback(sock, client);
                sock->Close();
                exit(OK);
            }
            else
            {
                //父进程 -> sock
                sock->Close();
                pid_t rid = ::waitpid(id, nullptr, 0);
                (void)rid;
            }
        }

        _isrunning = false;
    }

    ~TcpServer(){}

private:
    uint16_t _port;
    std::unique_ptr<Socket> _listensockptr;
    bool _isrunning;
    ioservice_t _service;
};